import js from '@eslint/js';
import eslintPrettier from 'eslint-plugin-prettier';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';
import importSort from 'eslint-plugin-simple-import-sort';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import unusedImport from 'eslint-plugin-unused-imports';

const frontendBuilderConfig = {
    extends: [js.configs.recommended, ...tseslint.configs.recommended],
    files: ['apps/frontend/**/*.{ts,tsx}'],
    languageOptions: {
        ecmaVersion: 2020,
        globals: globals.browser,
    },
    plugins: {
        'react-hooks': reactHooks,
        'react-refresh': reactRefresh,
    },
    rules: {
        ...reactHooks.configs.recommended.rules,
        'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
        'react/prop-types': 'off',
        'react/react-in-jsx-scope': 'off',
        '@typescript-eslint/unbound-method': 'off',
        '@typescript-eslint/no-misused-promises': 'off',
        '@typescript-eslint/no-unsafe-member-access': 'off',
    },
};

const backendBuilderConfig = {
    files: ['apps/backend/**/*.ts'],
    languageOptions: {
        globals: {
            ...globals.node,
            ...globals.jest,
        },
        parser: tseslint.parser,
    },
    rules: {
        '@typescript-eslint/explicit-function-return-type': 'off',
        '@typescript-eslint/explicit-module-boundary-types': 'off',
        '@typescript-eslint/interface-name-prefix': 'off',
        '@typescript-eslint/no-explicit-any': 'off',
    },
};

export default tseslint.config(
    {
        ignores: ['**/dist/**', '**/build/**', '**/*.d.ts'],
    },
    {
        extends: [js.configs.recommended, ...tseslint.configs.recommended],
        files: ['**/*.{js,jsx,ts,tsx}'],
        plugins: {
            prettier: eslintPrettier,
            'simple-import-sort': importSort,
            'unused-imports': unusedImport,
        },
        rules: {
            '@typescript-eslint/no-explicit-any': 'off',
            'prettier/prettier': 'error',
            'simple-import-sort/imports': 'error',
            'simple-import-sort/exports': 'error',
            'unused-imports/no-unused-imports': 'error',
            "no-console": ["error", { "allow": ["log"] }],
            'unused-imports/no-unused-vars': [
                'warn',
                {
                    vars: 'all',
                    varsIgnorePattern: '^_',
                    args: 'after-used',
                    argsIgnorePattern: '^_',
                },
            ],
        },
        languageOptions: {
            parser: tseslint.parser,
            globals: {
                ...globals.node,
            },
            parserOptions: {
                // project，有哪些子包是你的项目
                project: ['./tsconfig.client.json', './tsconfig.server.json', '**/*/tsconfig.json'],
                // tsconfigRootDir，tsconfig.json文件的根目录
                tsconfigRootDir: import.meta.dirname,
            },
        },
    },
    frontendBuilderConfig,
    backendBuilderConfig
);
